package org.dfrt.own.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dfrt.own.entity.Blog;
import org.dfrt.own.entity.BlogExp;
import org.dfrt.own.entity.User;
import org.dfrt.own.mapper.BlogMapper;
import org.dfrt.own.service.BlogService;
import org.dfrt.own.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhang
 * ClassName: BlogServiceImpl
 * Description: 博客业务逻辑层实现类
 * date: 2023/10/6 17:56
 * @since JDK 8
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;


    /**
     * 上传文章
     *
     * @param user
     * @param btitle
     * @param bcontent
     * @return
     */
    @Override
    public int pubBlog(User user, String btitle, String bcontent) {
        Blog blog = new Blog();
        blog.setUid(user.getUid());
        blog.setBtime(CommonUtil.datetimeToStr(LocalDateTime.now()));
        blog.setBtitle(btitle);
        blog.setBcontent(bcontent);

        int i = blogMapper.insertBlog(blog);

        return i;
    }

    /**
     * 查找当前用户的博文 作者信息 评论信息
     *
     * @param user
     * @return
     */
    @Override
    public List<BlogExp> findBlogSplitPageByUser(User user) {

        List<BlogExp> blogList = blogMapper.selectBlogByUser(user);

        return blogList;
    }



    /*
    @Transactional 事务注解
     */

    /**
     * 删除博客文章
     *
     * @param bid
     * @return
     */
    @Override
    public int deleBlog(Integer bid) {
        int i = blogMapper.deleBlogById(bid);
        return i;
    }

    /**
     * 查询所有博客文章
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo findBlogSplitPage(Integer page, int pageSize) {
        //对起始页进行配置
        PageHelper.startPage(page, pageSize);

        List<BlogExp> blogList = blogMapper.findAllBlog();

        //对查询到的博文信息进行分页
        PageInfo<BlogExp> blogPage = new PageInfo<>(blogList);

        return blogPage;
    }

    /**
     * 查询博客总条数
     * @return
     */
    @Override
    public Integer findBlogSumPage() {
        int i = blogMapper.findUserBlogNum();
        return i;
    }

}
